﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace _18miljard.Partners
{
	public class PartnerCollector : XmlDocument
	{
		PartnerCollection partners = new PartnerCollection();
		DateTime now = DateTime.Now; //must be property, since the class is cached.

		public PartnerCollector()
		{
			this.ReadPartners();
		}

		private void ReadPartners()
		{
			string path = HttpContext.Current.Server.MapPath("/App_Data/Partners.xml");

			this.Load(path);

			XmlNodeList nodes = this.SelectNodes("/miljard/partners/partner");

			foreach (XmlNode node in nodes)
			{
				this.ReadPartner(node);
			}
		}

		private void ReadPartner(XmlNode node)
		{
			XmlAttribute attrStartDate = node.Attributes["startdate"];
			XmlAttribute attrEndDate = node.Attributes["enddate"];

			if (attrStartDate == null || attrEndDate == null)
				return;

			DateTime startDate = DateTime.Parse(attrStartDate.Value);
			DateTime endDate = DateTime.Parse(attrEndDate.Value);

			if (startDate > this.now || endDate < this.now)
				return;

			LinkType linkType = LinkType.Banner;

			XmlAttribute attrType = node.Attributes["type"];

			if (attrType != null)
				if (attrType.Value == "text")
					linkType = LinkType.Text;

			if (linkType == LinkType.Banner)
				this.ReadBanner(node);
			else
				this.ReadText(node);
		}

		private void ReadBanner(XmlNode node)
		{
			string href = null;
			string banner = null;
			string alt = null;

			foreach (XmlNode childNode in node.ChildNodes)
			{
				switch (childNode.LocalName)
				{
					case "href":
						XmlAttribute hrefAttr = childNode.Attributes["url"];
						if (hrefAttr != null)
							href = hrefAttr.Value;
						break;

					case "banner":
						XmlAttribute urlAttr = childNode.Attributes["url"];
						if (urlAttr != null)
							banner = urlAttr.Value;

						XmlAttribute altAttr = childNode.Attributes["alt"];
						if (altAttr != null)
							alt = altAttr.Value;
						break;
				}
			}

			if (href != null && banner != null && alt != null)
				this.partners.Add(new Partner(href, banner, alt));
		}

		private void ReadText(XmlNode node)
		{
			string href = null;
			string text = null;
			string prefix = null;
			string suffix = null;

			foreach (XmlNode childNode in node.ChildNodes)
			{
				switch (childNode.LocalName)
				{
					case "href":
						XmlAttribute hrefAttr = childNode.Attributes["url"];
						if (hrefAttr != null)
							href = hrefAttr.Value;
						break;

					case "text":
						XmlAttribute prefixAttr = childNode.Attributes["prefix"];
						if (prefixAttr != null)
							prefix = prefixAttr.Value;

						XmlAttribute textAttr = childNode.Attributes["text"];
						if (textAttr != null)
							text = textAttr.Value;

						XmlAttribute suffixAttr = childNode.Attributes["suffix"];
						if (suffixAttr != null)
							suffix = suffixAttr.Value;

						break;
				}
			}

			if (href != null && text != null)
				this.partners.Add(new Partner(href, text, prefix, suffix));
		}

		public PartnerCollection Partners
		{
			get { return this.partners; }
		}
	}
}